using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AUSRIS.Framework.Logging;
using AUSRIS.Reports.Extensions.Security.Authentication;

namespace AUSRIS.Reports.Extensions.Security.UI
{
    public partial class AUSRISLogin : System.Web.UI.Page
    {
        private ILoginProvider loginProvider;
        private IAuthenticationProvider authenticationProvider;

        protected override void OnInit(EventArgs e)
        {
            try
            {
                this.loginProvider = ProviderFactory.GetProvider<ILoginProvider>();
                this.authenticationProvider = ProviderFactory.GetProvider<IAuthenticationProvider>();
            }
            catch(Exception ex)
            {
                Log.Fatal(Log.GetSource(), ex.ToString());
            }
            base.OnInit(e);
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if(this.authenticationProvider.IsUIEnabled)
                {
                    this.DisplayLoginForm();
                }
                else
                {
                    this.TryAutoLogin();
                }
            }
            catch(Exception ex)
            {
                if(!ex.GetType().Equals(typeof(ThreadAbortException)))
                {
                    Response.Write("Error :" + ex.Message);
                    Log.Fatal(Log.GetSource(), ex.ToString());
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string username = this.authenticationProvider.GetUsername(HttpContext.Current);
            if(!string.IsNullOrEmpty(username))
            {
                this.loginProvider.LoginUser(username);
                this.RedirectAuthenticatedUser();
            }
            else
            {
                this.lblMessage.Visible = true;
                this.lblMessage.Text = "Invalid username or password.";
            }
        }

        private void TryAutoLogin()
        {
            string userName = this.authenticationProvider.GetUsername(HttpContext.Current);
            if(!string.IsNullOrEmpty(userName))
            {
                this.loginProvider.LoginUser(userName);
                this.RedirectAuthenticatedUser();
            }
            else
            {
                string redirectUrl = this.authenticationProvider.GetRedirectUrl(HttpContext.Current);
                Response.Redirect(redirectUrl);
            }
        }

        private void RedirectAuthenticatedUser()
        {
            if(Request.QueryString["ReturnUrl"] != null)
            {
                Response.Redirect(Request.QueryString["ReturnUrl"]);
            }
            else
            {
                Response.Redirect("~/");
            }
        }

        private void DisplayLoginForm()
        {
            this.pnlForm.Visible = true;
        }
    }
}
